import {getResource, getUserPlayList, getlikelist, getUserInfo} from '../network/profile'

export const profile = {
  state: {
    userstate:0, //登录状态
    userinfo:null, //登录用户信息

    recommend: [],  //个人推荐歌单
    playlist:[],   //个人的歌单
    userlikemusicid:[], //个人喜欢的音乐列表的音乐ID：为了区分是否喜欢
  },
  mutations: {
    /**
     * 获取推荐歌单
     */
    GETRECOMMEND(state,payload) {
      state.recommend = payload
    },

    /**
     * 获取用户歌单
     */
    GETPLAYLIST(state,payload) {
      state.playlist = payload
    },

    /**
     * 获取个人喜欢音乐列表的音乐ID
     */
     GETLIKELISTID(state,payload) {
      state.userlikemusicid = payload.ids;
      //console.log(state.userlikemusicid);
    },
    /**
     * 退出登录删除用户数据
     */
    DELUSERINFO(state) {
      state.playlist = [], //清除个人歌单
      state.userlikemusicid = [], //清除个人喜欢歌单
      state.userinfo = null, //清除个人信息
      state.userstate = 0  //清除登录状态
    },
    UserState(state,payload) {
      state.userstate = payload
    },
    mUserInfo(state, payload) {
      state.userinfo = payload.data
      //console.log(payload.data);
      //console.log(state.userinfo);
    },
  },
  actions: {
    /**
     * 获取推荐歌单
     */
    getrecommend(context) {
      getResource().then(res => {
        context.commit("GETRECOMMEND",res.data)
      })
    },

    /**
     * 获取用户歌单（我的喜欢，创建歌单，收藏歌单）
     */
    getplaylist(context) {
      //console.log(this.state.userinfo.profile.userId);
      //console.log(context.state.userinfo);
      getUserPlayList(context.state.userinfo.profile.userId).then(res => {
        context.commit("GETPLAYLIST",res.data.playlist)
      })
    },
    
    /**
     * 获取个人喜欢音乐列表的音乐ID
     */
     getlikelistid(context) {
      getlikelist(context.state.userinfo.profile.userId).then(res => {
        //console.log(res.data);
        context.commit("GETLIKELISTID",res.data)
      }) 
     },
     //获取用户信息
    getuserinfoapi(context){
       return getUserInfo().then(res => {
        context.commit('mUserInfo',res)
        return res.data
        //console.log(res.data);
        //console.log(context.state.userinfo.profile.userId);
      })
    },

  }
}